Socket
Socket
Sign inDemoInstall

child_process

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

child_process

This package name is not currently in use, but was formerly occupied by another package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.


Version published
Weekly downloads
773K
increased by3.02%
Maintainers
1
Weekly downloads
 
Created

What is child_process?

The 'child_process' module in Node.js allows you to spawn new processes, execute commands, and communicate with those processes. It provides a way to run shell commands, scripts, and other programs from within a Node.js application.

What are child_process's main functionalities?

Spawning a new process

The 'spawn' function launches a new process with a given command. In this example, it runs the 'ls' command with the '-lh' and '/usr' arguments. The stdout and stderr streams are used to handle the output and errors, respectively.

const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

Executing a command

The 'exec' function runs a command in a shell and buffers the output. This example executes the 'ls -lh /usr' command and logs the stdout and stderr outputs. If an error occurs, it is logged as well.

const { exec } = require('child_process');
exec('ls -lh /usr', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

Forking a new Node.js process

The 'fork' function is a special case of 'spawn' used to create new Node.js processes. It allows for communication between the parent and child processes using the 'message' event. In this example, a new Node.js process is created to run 'child.js', and messages are exchanged between the parent and child.

const { fork } = require('child_process');
const child = fork('child.js');

child.on('message', (msg) => {
  console.log('Message from child', msg);
});

child.send({ hello: 'world' });

Other packages similar to child_process

FAQs

Package last updated on 26 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc